<!-- 旅游商品库存配置 -->
<template>
    <div >
        <a-page-header
            style="border: 1px solid rgb(235, 237, 240);background:#fff"
            :title="goodsName+'的库存配置'"
            @back="() => $router.go(-1)"
        >
            <div class='table-page-search-wrapper'>
                <a-form layout='inline'>
                <a-row :gutter='48'>
                        <a-col :md="8" :sm="24">
                            <a-form-item label="宠物门店">
                                <a-select v-model="storeId" placeholder="请选择" allowClear @change="getData">
                                    <a-select-option v-for="(item,i) in storeList" :key="i" :value="item.id">{{item.name}}-{{item.town}}-{{item.detailedAddress}}</a-select-option>
                                </a-select>
                            </a-form-item>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                        </a-col>
                        <a-col :md='8' :sm='24'>
                            <a-form-item >
                                <a-button type='primary' style="margin-right: 20px" @click="getData">查询</a-button>
                                <a-button type="" icon="plus" @click="add()">添加</a-button>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-form>
            </div>  

            <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size, }" size="small">
                <div slot="storeId" slot-scope="storeId">
                    <span v-for="(item,i) in storeList" :key="item.id">
                        <a-tag v-if="item.id == storeId" :color="colors[i%13]">{{item.name}}</a-tag>
                    </span>
                </div>
                <div slot="operation" slot-scope="text,record">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前配置?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </a-table>
        </a-page-header>

        <a-modal
            title="添加、修改旅游商品库存"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="宠物门店" >
                        <a-select v-model="item.storeId" placeholder="请选择宠物门店" allowClear>
                            <a-select-option v-for="(item,i) in storeList" :key="i" :value="item.id.toString()">{{item.name}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="商品数量">
                        <a-input v-model="item.quantity" placeholder="请输入商品数量" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="操作">
                        <a-radio-group v-model="item.type" >
                            <a-radio  v-for="(item,i) in typeList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryPetGoodsStock , updatePetGoodsStock , queryPetStore } from '@/api/pet';
import { columns } from './table';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            goodsName:this.$route.query.goodsName,
            goodsId:this.$route.query.goodsId,
            storeIds: [],
            storeId: undefined,
            storeList: [],
            columns,
            columns,
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            item: {
                id: '',
                storeId: undefined,
                quantity: '', // 数量
                type: '6', // 操作类型
            },
            typeList: [
                { id: '6', name: '增加库存'},
                { id: '7', name: '减少库存'},
            ],
            confirmLoading: false,
            isAdd: true,
            visible: false,
        };
    },
    async created(){
        try {
            this.storeIds = this.$route.query.storeId.split(',');
            await this.queryStore();
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 查询宠物门店
        queryStore(){
            let _this = this
            return new Promise((resolve,reject) => {
                queryPetStore({'size': 99,'page': 0})
                .then(res=>{
                    if(res.success){
                        var list = [];
                        for (let index = 0; index < res.data.content.length; index++) {
                            const element = res.data.content[index];
                            if(_this.storeIds.find(itm => itm == element.id)){
                                list.push(element);
                            }
                        }
                        _this.storeList = list;
                        resolve()
                    }else{
                        reject(res.errMessage)
                    }
                })
            })
        },
        // 查询宠物商品列表
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId': _this.global.brandId,
                'name': _this.name,
                'storeId': _this.storeId,
                'goodsId': _this.goodsId,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryPetGoodsStock(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true;
            this.item.id = '';
            this.item.storeId = undefined;
            this.item.quantity = '';
            this.item.type = '6';
            this.visible = true
        },
        // 修改
        update(item){
            let _this = this;
            item.type = '6';
            item.quantity = '';
            _this.item = JSON.parse(JSON.stringify(item))
            _this.isAdd = false;
            _this.visible = true
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            if(_this.item.storeId == undefined){
                _this.$message.warning('请选择宠物门店')
                return;
            }
            if(_this.item.quantity == ''){
                _this.$message.warning('请输入操作数量')
                return;
            }
            const params = {
                'id': _this.item.id,
                'goodsId': _this.goodsId,
                'storeId': _this.item.storeId,
                'quantity': _this.item.quantity,
                'type': _this.item.type,
                'brandId': _this.brandId,
            }
            updatePetGoodsStock(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false;
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.type == '6' ? '添加成功' : '减少成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id': item.id,
                'goodsId': _this.goodsId,
                'storeId': item.storeId,
                'quantity': item.surplusStock,
                'type': '7',
                'isDelete': '1'
            }
            updatePetGoodsStock(params)
            .then(res=>{
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>